/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.network.tcp.tests;

import java.io.IOException;
import java.net.URI;

import org.latestbit.smartobjects.exceptions.IncorrectURIException;
import org.latestbit.smartobjects.network.*;
import org.latestbit.smartobjects.network.tcp.*;

public class TCPTestServer {
	private IAcceptor acceptor;
	private AcceptorListenerTester listener;
	private INetworkFactory networkFactory;

	public TCPTestServer(URI serverURI) throws IncorrectURIException, IOException {
		createAcceptor(serverURI);
	}

	public TCPTestServer(URI serverURI, INetworkFactory factory) throws IncorrectURIException, IOException {
		this.networkFactory = factory;
		createAcceptor(serverURI);		
	}
	
	private void createAcceptor(URI serverURI) throws IncorrectURIException, IOException {
		INetworkFactory factory = networkFactory;
		if(factory == null)
			factory = new TCPFactory();
		setAcceptor(factory.createAcceptor(serverURI));
		setListener(new AcceptorListenerTester());
		this.acceptor.addListener(listener);
	}

	public void setAcceptor(IAcceptor acceptor) {
		this.acceptor = acceptor;
	}

	public IAcceptor getAcceptor() {
		return acceptor;
	}

	public void setListener(AcceptorListenerTester listener) {
		this.listener = listener;
	}

	public AcceptorListenerTester getListener() {
		return listener;
	}
}
